/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: BaseProcessor
 * Author:   zhouheng
 * Date:     2019/5/15 11:01
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.star.pay.service.core.processor;

import com.star.pay.service.core.processor.AbstractProcessor;
import com.star.pay.service.core.processor.channel.PayChannelProxy;
import com.star.pay.service.core.processor.context.PaymentContext;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/5/15
 * @since 1.0.0
 */

public abstract class BaseProcessor extends AbstractProcessor {

    ThreadLocal<PayChannelProxy> threadLocal = new ThreadLocal<PayChannelProxy>();

    @Override
    protected boolean processTransaction(PaymentContext paymentContext) {
        return getThreadLocal().get().paymentProcess(paymentContext);
    }

    @Override
    protected boolean preCheckContext(PaymentContext paymentContext) {
        return getThreadLocal().get().preCheck(paymentContext);
    }

    @Override
    protected void initPayChannel(PaymentContext paymentContext) {
        threadLocal.set(PayChannelProxy.payChannelMap.get(paymentContext.getPayChannelEnum().getCode()));
    }

    @Override
    protected void clean(PaymentContext paymentContext) {
        getThreadLocal().remove();
    }

    public ThreadLocal<PayChannelProxy> getThreadLocal() {
        return threadLocal;
    }

    public void setThreadLocal(ThreadLocal<PayChannelProxy> threadLocal) {
        this.threadLocal = threadLocal;
    }
}